package app.netant.bt.btcat;

import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.enviroment.Enviroment;
import cn.lily.com.lang.number.MathUtil;

public class BtCatNetRoot {
	
	private static String pageUrl = "https://btcat.org/search/%E6%80%A7%E6%AC%B2/default-";
	
	public static void analyse(int minIdx,int maxIdx){
		
		System.out.println("整体处理开始........");
		
		//断点续传
		resume();
		
		for(int i=minIdx;i<=maxIdx;i++){
			
			String url = pageUrl + i+".html";
				
			System.out.println("第"+i+"页数据开始处理！");
			
			try {
				BtCatNetPage page = new BtCatNetPage(url);
				page.analyse();
			} catch (Exception e) {
				e.printStackTrace();
				DBFactory.getThreadDB().executeUpdate("insert into ${schema}.LEO_BTCAT_RES_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
										+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+url+"','1') ");					
				continue;
			}
			
		}
		
		System.out.println("整体处理结束........");
		
	}
	
	public static void analyse(int maxIdx){
		
		System.out.println("整体处理开始........");
		
		//断点续传
		resume();
		
		for(int i=1;i<=maxIdx;i++){
			
			String url = pageUrl + i+".html";
				
			System.out.println("第"+i+"页数据开始处理！");
			
			try {
				BtCatNetPage page = new BtCatNetPage(url);
				page.analyse();
			} catch (Exception e) {
				e.printStackTrace();
				DBFactory.getThreadDB().executeUpdate("insert into ${schema}.LEO_BTCAT_RES_EXCEP (EXCEP_ID,EXCEP_URL,EXCEP_LEVEL) "
										+ "values ('"+System.currentTimeMillis()+""+MathUtil.random(3)+"','"+url+"','1') ");					
				continue;
			}
			
		}
		
		System.out.println("整体处理结束........");
		
	}
	
	/**
	 * 断点续传
	 */
	public static void resume(){
		
		System.out.println("断点续传处理开始........");
		
		DBFactory.getThreadDB().executeUpdate("delete from ${schema}.LEO_BTCAT_RES_INFO "
				+ "where RES_URL in "
				+ "(select EXCEP_URL from ${schema}.LEO_BTCAT_RES_EXCEP)");
		
		DBFactory.getThreadDB().executeUpdate("delete from ${schema}.LEO_BTCAT_RES_EXCEP");
		
		System.out.println("断点续传处理结束........");
		
	}
	
	/**
	 * 纠正错误数据
	 */
	public static void error(){
		
		System.out.println("清理错误数据........");
		
		DBFactory.getThreadDB().executeUpdate("delete from ${schema}.LEO_BTCAT_RES_INFO "
				                   + "where RES_URL in "
				                   + "(select EXCEP_URL from ${schema}.LEO_BTCAT_RES_EXCEP)");
		
		System.out.println("异常处理开始........");
		
		DataTable table = DBFactory.getThreadDB().executeQueryTable("select * from ${schema}.LEO_BTCAT_RES_EXCEP");
		while(table.next()){
			try {
				
				String expID = table.getString("EXCEP_ID");
				String expUrl = table.getString("EXCEP_URL");
				String expLevel = table.getString("EXCEP_LEVEL");
				
				System.out.println("异常地址："+expUrl+"开始处理.......");
				
				if("1".equals(expLevel)){
					
					BtCatNetPage page = new BtCatNetPage(expUrl);
					page.analyse();
					
				}else if("2".equals(expLevel)){
					
					Enviroment.getEnv().getLogBean().beginLog();

					DataTable dataTable = DBFactory.getThreadDB().executeQueryTable("select * from ${schema}.LEO_BTCAT_RES_INFO where RES_URL='"+expUrl+"'");
					if(dataTable.next()){
						continue;
					}
					
					BtCatEntity entity = new BtCatEntity();
					entity.setResUrl(expUrl);
					
					BtCatNetDetail detail = new BtCatNetDetail(expUrl);
					detail.setEntity(entity);
					detail.analyse();
					
					entity.insert();
					
					Enviroment.getEnv().getLogBean().endLog();
					
				}
					
				DBFactory.getThreadDB().executeUpdate("delete from ${schema}.LEO_BTCAT_RES_EXCEP where EXCEP_URL='"+expUrl+"'");
				
			} catch (Exception e) {
				e.printStackTrace();
				continue;
			}
		}
		
		System.out.println("异常处理结束........");
		
	}
	
	public static void main(String[] args) {
		
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		
		//全更新259-851
		//BtCatNetRoot.analyse(851);
		BtCatNetRoot.error();
		
		
	}
	
}
